package com.lvyq.javaprint.exception;

import cn.hutool.json.JSONUtil;
import com.lvyq.javaprint.common.CommonResult;
import com.lvyq.javaprint.common.GlobalCode;
import com.lvyq.javaprint.common.ResultCode;
import com.lvyq.javaprint.ws.WebSocketServer;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @author lvyq  全局异常
 * @version 1.0
 * Create by 2024-08-06 16:07
 */
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult globalException(Exception e){

        if (e instanceof NullPointerException){
           return CommonResult.error(new ResultCode(20001,"空指针异常"));
        }

        //不合法参数
        if (e instanceof IllegalArgumentException){
            return CommonResult.error(new ResultCode(20002,e.getLocalizedMessage()));
        }
        //不合法参数
        if (e instanceof RuntimeException){
            return CommonResult.error(new ResultCode(20003,e.getLocalizedMessage()));
        }

        return CommonResult.error(new ResultCode(20004,e.getLocalizedMessage()));
    }
}